package lab03.test14;

import java.sql.*;

public class Test {
    public static void main(String[] args) {
       Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java_2017?" +
                    "serverTimezone=UTC&characterEncoding=utf-8", "root", "123456");
            statement=connection.createStatement();
           /* statement.executeUpdate("create table userinfo(" +
                    "usercode  varchar(4) not null primary key," +
                    "username varchar(10)," +
                    "password varchar(6)," +
                    "email  varchar(40))"
                    );*/
            /*statement.executeUpdate("INSERT INTO userinfo (usercode,username,password,email)" +
                    "SELECT '7101','宋晓梅','123456','songjie@263.net' UNION " +
                    "SELECT '7102','关松杰','654321','songjie@263.net' UNION " +
                    "SELECT '7103','李晓梅','123654','xiaomei@yahoo.com.cn' UNION " +
                    "SELECT '7104','张松岩','456123','songyan@yahoo.com.cn'UNION " +
                    "SELECT '7105','王亚杰','321456','yajie@hotmail.com' UNION " +
                    "SELECT '7106','曲小声','654321','xiaosheng@263.net'");*/
                     resultSet=statement.executeQuery("SELECT * FROM userinfo");
                     while (resultSet.next()){
                         System.out.print("\t"+resultSet.getString(1));
                         System.out.print("\t"+resultSet.getString(2));
                         System.out.print("\t"+resultSet.getString(3));
                         System.out.println("\t"+resultSet.getString(4));
                     }
                  //  statement.executeUpdate( "INSERT INTO userinfo VALUES ('7107'," +
                          //  "'宋晓梅','123456','songjie@263.net')");
            System.out.println("-----------------------------------------------------");
            resultSet=statement.executeQuery("SELECT * FROM userinfo");
            while (resultSet.next()){
                System.out.print("\t"+resultSet.getString(1));
                System.out.print("\t"+resultSet.getString(2));
                System.out.print("\t"+resultSet.getString(3));
                System.out.println("\t"+resultSet.getString(4));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                connection.close();
                statement.close();
                resultSet.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
